//
//  UserAccount.swift
//  WeiBo
//
//  Created by tusm on 2017/6/26.
//  Copyright © 2017年 GM. All rights reserved.
//

import UIKit


/// 用户模型
class UserAccount: NSObject, NSCoding {
    
    //用户授权的唯一票据，用于调用微博的开放接口
    var access_token:String?
    //access_token的生命周期，单位是秒数
    var expires_in:TimeInterval = 0{
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //到期日期，计算所得
    var expires_date:NSDate?
    //授权用户的UID
    var uid:String?
    //用户昵称
    var screen_name:String?
    //用户图像
    var avatar_large:String?
    
    init(dict: [String:AnyObject]) {
        
        super.init()
        
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    override var description: String{
        
        let keys = ["access_token","expires_date","uid","screen_name","avatar_large"]
        
        return dictionaryWithValues(forKeys: keys).description
    }
    
    //MARK:归档
    public func encode(with aCoder: NSCoder){
        
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        
    }
    //MARK:解档
    public required init?(coder aDecoder: NSCoder){
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? NSDate
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        
        
    }

}
